package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.FrameLayout;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.fragment.Fragment_ChooseDatiType;
import com.xgkj.diyiketang.fragment.Fragment_Xuanze;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.CreateQuestionTask;
import com.xgkj.diyiketang.util.ToastUtil;
import com.xgkj.diyiketang.util.ViewUtil;

public class CreateQuestionActivity extends BaseActivity implements Fragment_Xuanze.NextOnClickListener ,Fragment_ChooseDatiType.DatiFragmentClickListener{
    private Context mContext;
    private FrameLayout mContentLayout;
    private Fragment_ChooseDatiType chooseFragment;
    private Fragment_Xuanze createXuanzeFragment;
    private static final String FRAGMENT_CHOOSE_KEY = "chooseFragment";
    private static final String FRAGMENT_XUANZE_KEY = "xuanzeFragment";

    private String mSubjectNo;
    private int rewardCount;
    private String grade;
    private int isopen = 0;
    public String questionType;
    private String answerType;

    private String questionUrl;
    private String answerUrl;
    private String rightAnswer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_question);
        mContext = this;
        mContentLayout = (FrameLayout) findViewById(R.id.neirong);
        restoreFragment(savedInstanceState);
        initFragment();
        replaceFragment(FRAGMENT_CHOOSE_KEY,chooseFragment);
    }

    private void replaceFragment(String tag, Fragment mFragment) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        if (chooseFragment.isAdded()){
            fragmentTransaction.hide(chooseFragment);
        }

        if (createXuanzeFragment.isAdded()){
            fragmentTransaction.hide(createXuanzeFragment);
        }

        if (mFragment.isAdded()){
            fragmentTransaction.show(mFragment);
        }else {
            fragmentTransaction.add(R.id.neirong,mFragment,tag);
        }
        if (tag.equals(FRAGMENT_XUANZE_KEY)){
            fragmentTransaction.addToBackStack(null);
        }

        fragmentTransaction.commit();
    }

    private void initFragment() {
        if (chooseFragment == null){
            chooseFragment = new Fragment_ChooseDatiType();
            chooseFragment.setDatiFragmentClickLitener(this);
        }

        if (createXuanzeFragment == null){
            createXuanzeFragment = new Fragment_Xuanze();
            createXuanzeFragment.setNextOnclickListener(this);
        }
    }

    private void restoreFragment(Bundle savedInstanceState) {
        if (savedInstanceState != null){
            FragmentManager fm = getSupportFragmentManager();
            chooseFragment = (Fragment_ChooseDatiType) fm.findFragmentByTag(FRAGMENT_CHOOSE_KEY);
            createXuanzeFragment = (Fragment_Xuanze) fm.findFragmentByTag(FRAGMENT_XUANZE_KEY);
        }
    }

    @Override
    public void onNextClick() {
        questionUrl = createXuanzeFragment.getQuestionUrl();
        answerUrl = createXuanzeFragment.getAnswerUrl();
        rightAnswer = createXuanzeFragment.getRightAnswer();
        if (ViewUtil.isNull(questionUrl) || ViewUtil.isNull(answerUrl) || ViewUtil.isNull(rightAnswer)){
            ToastUtil.showMessage(mContext,R.string.please_check_your_information);
            return;
        }

        doCreateQuestion();
    }

    private void doCreateQuestion() {
        CreateQuestionTask createQuestionTask = new CreateQuestionTask(mSubjectNo,rewardCount,grade,String.valueOf(isopen),questionUrl,rightAnswer,answerUrl,questionType,answerType);
        createQuestionTask.addListener(mListener);
        getEatShowApp().getTaskManager().addTask(createQuestionTask);
    }

    private TaskEventListener mListener = new ShowToastTaskEventListener(this){
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            ToastUtil.showMessage(mContext,R.string.create_question_success);
            finish();
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
        }
    };

    @Override
    public void onBackClick() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.popBackStackImmediate();
    }

    @Override
    public void onDatiFragmentClick() {
        //下一步
        mSubjectNo = chooseFragment.getmSubjectNo();
        rewardCount = chooseFragment.getRewardCount();
        grade = chooseFragment.getGrade();
        isopen = chooseFragment.isopen();
        questionType = chooseFragment.getQuestionType();
        answerType = chooseFragment.getAnswerType();
        if (rewardCount<=0 || ViewUtil.isNull(mSubjectNo) || ViewUtil.isNull(grade) || ViewUtil.isNull(questionType) || ViewUtil.isNull(answerType)){
            ToastUtil.showMessage(mContext,R.string.please_check_your_information);
            return;
        }

        replaceFragment(FRAGMENT_XUANZE_KEY,createXuanzeFragment);

    }
}
